import list2tree from 'list2tree'
import { deleteNode, getListAll } from './api'

const getNavTreeDataFunc = list2tree({
  idKey: 'id',
  parentIdKey: 'pid',
  newKey: {
    key: 'id',
    value: 'id',
    title: 'name',
  },
})

export default {
  namespace: 'realTimeScriptDev',
  state: {
    navList: [],
    treeList: [],
    tasks: [],
    taskIds: [],
    taskId: null,
    curstoMvariablesArrMr: [],
    curstoMvariablesArr: [],
    activeKey: '',
    treeCurrentTargetNode: {},
  },
  effects: {
    *getTree({ params, cb }, { call, put }) {
      const res = yield call(getListAll, params)
      let arr = []
      if (res.data && res.data.length) {
        arr = res.data
      }
      yield put({
        type: 'save',
        payload: {
          navList: arr || [],
          treeList: getNavTreeDataFunc(arr),
        },
      })
      if (cb) {
        cb(res)
      }
    },
    *upFlow({ params, cb }, { call, put }) {
      yield put({ type: 'save', payload: { tasks: params.tasks } })
    },
    *setSelectedKey({ params, cb }, { call, put }) {
      yield put({ type: 'save', payload: { taskId: params.selectedKeys, treeCurrentTargetNode: params.treeCurrentTargetNode } })
      cb && cb()
    },
    *deleteNode({ params, cb }, { call, put }) {
      const res = yield call(deleteNode, params)
      if (cb) {
        cb(res)
      }
    },
    * changeActiveKey({ params, cb }, { put }) {
      // 切换tab页
      yield put({ type: 'setActiveKey', payload: { id: params.id, treeCurrentTargetNode: params?.treeCurrentTargetNode } })
      cb && cb()
    },
    * taskTabsHandle({ params, cb }, { put }) {
      // 打开任务或者切换任务 处理
      yield put({ type: 'addTaskTabs', payload: { id: params.id, item: params.item } })
      cb && cb()
    },
    * deleteTaskId({ params, cb }, { put }) {
      // 关闭某个任务
      yield put({ type: 'closeTaskTabs', payload: { id: params.id } })
      cb && cb()
    },
    * deleteAllTaskId({ params, cb }, { put }) {
      // 关闭其他
      let ids
      if (params && params.ids) ids = params.ids
      yield put({ type: 'closeAllTaskTabs', payload: { ids } })
      cb && cb()
    },
    * setTaskTabsEdie({ params }, { put }) {
      // 设置某个任务的编辑状态
      yield put({ type: 'setTaskEdited', payload: { item: params.item } })
    },
    * curstoMvariablesMr({ params }, { put }) {
      // 设置某个任务的编辑状态
      yield put({ type: 'setCurstoMvariablesMr', payload: { obj: params } })
    },
    * curstoMvariablesTask({ params }, { put }) {
      // 设置某个任务的编辑状态
      yield put({ type: 'setCurstoMvariablesTask', payload: { obj: params } })
    },
  },
  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      }
    },
    setActiveKey(state, { payload }) {
      return {
        ...state,
        ...{
          activeKey: payload.id,
          treeCurrentTargetNode: payload?.treeCurrentTargetNode,
        },
      }
    },
    addTaskTabs(state, { payload }) {
      const { taskIds, navList, tasks } = state
      const newArr = taskIds.concat([])
      const activeKey = String(payload.id)
      if (taskIds.indexOf(activeKey) === -1) {
        newArr.push(activeKey)
      }
      const tempArr = []
      newArr.map((id) => {
        const obj = tasks.find((item) => item.id == id)
        if (obj) {
          tempArr.push({ ...obj })
        } else if (payload.item) {
          tempArr.push({ ...payload.item })
        } else {
          const newObj = navList.find((item) => item.id == id)
          if (newObj) tempArr.push({ ...newObj })
        }
      })
      return {
        ...state,
        ...{
          taskIds: newArr,
          tasks: tempArr,
          activeKey,
        },
      }
    },
    closeTaskTabs(state, { payload }) {
      const {
        taskIds, navList, tasks, activeKey,
      } = state
      const newArr = []
      taskIds.map((id) => {
        if (id != payload.id) {
          newArr.push(id)
        }
      })
      let newActiveKey = activeKey
      const tempArr = []
      newArr.map((id) => {
        const obj = tasks.find((item) => item.id == id)
        if (obj) {
          if (activeKey == payload.id) {
            newActiveKey = id
          }
          tempArr.push({ ...obj })
        } else {
          const newObj = navList.find((item) => item.id == id)
          if (newObj) tempArr.push({ ...newObj })
        }
      })
      return {
        ...state,
        ...{
          taskIds: newArr,
          tasks: tempArr,
          activeKey: newActiveKey,
        },
      }
    },
    closeAllTaskTabs(state, { payload }) {
      const { navList, tasks } = state
      const newArr = []
      const tempArr = []
      let activeKey
      if (payload.ids) {
        payload.ids.map((id) => {
          newArr.push(id)
          const obj = tasks.find((item) => item.id == id)
          if (obj) {
            activeKey = id
            tempArr.push({ ...obj })
          } else {
            const newObj = navList.find((item) => item.id == id)
            if (newObj) tempArr.push({ ...newObj })
          }
        })
      }
      return {
        ...state,
        ...{
          taskIds: newArr,
          tasks: tempArr,
          activeKey,
        },
      }
    },
    setTaskEdited(state, { payload }) {
      const { tasks } = state
      const newArr = tasks.map((item) => {
        if (item.id == payload.item.id) {
          return payload.item
        }
        return item
      })
      return {
        ...state,
        ...{
          tasks: newArr,
        },
      }
    },
    setCurstoMvariablesMr(state, { payload }) {
      const { curstoMvariablesArrMr } = state
      const newArr = []
      let bool = false
      if (payload.obj.bool) {
        curstoMvariablesArrMr.length
          ? curstoMvariablesArrMr.map((item, key) => {
            if (item.task.id == payload.obj.id) {
              const arr = []
              item.arr.length
                && item.arr.map((obj, k) => {
                  k == payload.obj.key
                    ? arr.push({
                      paramName: obj.paramName,
                      paramValue: payload.obj.paramValue,
                    })
                    : arr.push(obj)
                })
              newArr.push({
                task: item.task,
                arr,
              })
            } else {
              newArr.push(item)
            }
          })
          : null
      } else {
        curstoMvariablesArrMr.length
          ? curstoMvariablesArrMr.map((item) => {
            if (item.task.id == payload.obj.task.id) {
              const arr = []
              payload.obj.arr
                && payload.obj.arr.length
                && payload.obj.arr.map((obj, key) => {
                  if (item.arr[key] && item.arr[key].paramValue) {
                    arr.push({
                      paramName: obj.paramName,
                      paramValue: item.arr[key].paramValue,
                    })
                  } else {
                    arr.push({
                      paramName: obj.paramName,
                      paramValue: obj.paramValue,
                    })
                  }
                })
              newArr.push({
                task: payload.obj.task,
                arr,
              })
              bool = true
            } else {
              newArr.push(item)
            }
          })
          : null
        bool ? null : newArr.push(payload.obj)
      }
      return {
        ...state,
        ...{
          curstoMvariablesArrMr: newArr,
        },
      }
    },
    setCurstoMvariablesTask(state, { payload }) {
      const { curstoMvariablesArr } = state
      const newArr = []
      let bool = false
      if (payload.obj.bool) {
        curstoMvariablesArr.length
          ? curstoMvariablesArr.map((item, key) => {
            if (item.task.id == payload.obj.id) {
              const arr = []
              item.arr.length
                && item.arr.map((obj, k) => {
                  k == payload.obj.key
                    ? arr.push({
                      paramName: obj.paramName,
                      paramValue: payload.obj.paramValue,
                    })
                    : arr.push(obj)
                })
              newArr.push({
                task: item.task,
                arr,
              })
            } else {
              newArr.push(item)
            }
          })
          : null
      } else {
        curstoMvariablesArr.length
          ? curstoMvariablesArr.map((item) => {
            if (item.task.id == payload.obj.task.id) {
              const arr = []
              payload.obj.arr
                && payload.obj.arr.length
                && payload.obj.arr.map((obj, key) => {
                  if (item.arr[key] && item.arr[key].paramValue) {
                    arr.push({
                      paramName: obj.paramName,
                      paramValue: obj.paramValue
                        ? obj.paramValue
                        : item.arr[key].paramValue,
                    })
                  } else {
                    arr.push({
                      paramName: obj.paramName,
                      paramValue: obj.paramValue,
                    })
                  }
                })
              newArr.push({
                task: payload.obj.task,
                arr,
              })
              bool = true
            } else {
              newArr.push(item)
            }
          })
          : null
        bool ? null : newArr.push(payload.obj)
      }
      return {
        ...state,
        ...{
          curstoMvariablesArr: newArr,
        },
      }
    },
  },
}
